#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>

int main()
{
   int serv_sock,clnt_sock;
   struct sockaddr_in serv_addr,clnt_addr;
   socklen_t clt_sz = sizeof(clnt_addr);
   // ipv4 or ipv6, SOCK_STREAM or SOCK_DGRAM, tcp or udp
   // 0代表系统自动推断tcp or udp
   serv_sock = socket(PF_INET,SOCK_STREAM,0);
   memset(&serv_addr,0,sizeof(serv_addr));
   serv_addr.sin_family = AF_INET;
   serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
   serv_addr.sin_port = htons(8000);
   printf("running at 127.0.0.1:8000\n");
   bind(serv_sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
   listen(serv_sock,20);
   char str[] = "HTTP/1.1 200 OK\r\nServer: linux Web Server \r\nContent-length:11\r\ncontent-type: text/plain\r\n\r\nhello world";
    while (1)
    {
       clnt_sock = accept(serv_sock,(struct sockaddr*)&clnt_addr,&clt_sz);
       write(clnt_sock, str, sizeof(str));
       close(clnt_sock);
    }
   close(serv_sock);
   return 0;
}